package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:石小俊
 * @Date:2022/12/12 10:22
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
@WebServlet(urlPatterns = "/life3",loadOnStartup = 4,initParams = {
        @WebInitParam(name = "username", value= "admin"),
        @WebInitParam(name = "password", value= "123456")
})
public class LifeThreeServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("继承HttpServlet的子类的初始化方法");
    }

    // 当Servlet中重写了service方法之后
    // 则访问Servlet时不判断请求方式直接进入到service方法
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("LifeThreeServlet.service");
        String username = getInitParameter("username");
        String password = getInitParameter("password");
        System.out.println("username:"+username+",password:"+password);
    }

    // 当Servlet中没有重写service方法
    // 则访问Servlet时会根据访问的请求方式判断进入哪一个方法
    // post请求-->doPost
    // get请求-->doGet
    // ...
    // xxx请求-->doXxx
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("LifeThreeServlet.doPost");
//    }
//
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("LifeThreeServlet.doGet");
//    }
//
//    @Override
//    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("LifeThreeServlet.doDelete");
//    }
//
//    @Override
//    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("LifeThreeServlet.doPut");
//    }

    @Override
    public void destroy() {
        System.out.println("LifeThreeServlet.destroy");
    }
}
